/*
 * @Author: 江舟 jijiangzhou@recycloud.cn
 * @Date: 2024-04-09 14:38:25
 * @LastEditors: 江舟 jijiangzhou@recycloud.cn
 * @LastEditTime: 2024-08-09 10:19:49
 * @FilePath: \vite-project\src\store\common.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { create } from 'zustand';
import { listAllAvaliableValueOfType } from 'apis/common';

interface State {
    menuCollapsed: boolean;
    catTypeTree: any[];
    catTypeTreeLoading: boolean;
    carSpecification: {
        [c: string]: any;
    };
}

interface Action {
    updateMenuCollapsed: (collapsed: boolean) => void;
    getSpecification: (type: string) => void;
}

export const useCommonStore = create<State & Action>((set, get) => ({
    menuCollapsed: false,
    catTypeTree: [],
    catTypeTreeLoading: true,
    carSpecification: {},
    updateMenuCollapsed: (collapsed: boolean) => {
        set((state) => ({
            menuCollapsed: collapsed,
        }));
    },
    getSpecification(type: string) {
        const { carSpecification } = get();
        if (carSpecification?.type) {
            return;
        }
        listAllAvaliableValueOfType({
            type: 1,
            value: type,
        }).then((res) => {
            set((state) => ({
                carSpecification: {
                    ...carSpecification,
                    [type]: res?.values,
                },
            }));
        });
    },
}));
